package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Packing;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {
    @Reference
     private PackingService packingService;


    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "5")int pageSize){
        PageInfo<Packing> pageInfo = packingService.findByPage(pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/packing/packing-list";
    }

    /**
     * url:/cargo/packing/toPacking.do
     * 参数：选中id的一个集合
     */
    @RequestMapping("/toPacking")
    public String toExport(String id){
        //必须已报运的数据，才可以点击生成装箱单
        boolean flag = packingService.isShipment(id);
        if (flag){

            return "redirect:/cargo/export/list.do";
        }
        //获取一个pageing对象，用来数据回写
        Packing packing = packingService.getPacking(id);
        if (packing==null){
            return "cargo/packing/error";
        }

        request.setAttribute("packing",packing);
        request.setAttribute("id",id);
        return "cargo/packing/packing-toPacking";
    }

    @RequestMapping("/edit")
    public String edit(Packing packing){
        if (StringUtils.isEmpty(packing.getPackingListId())){
            packingService.save(packing);
        }
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String id){
        String msg = packingService.delete(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",msg);
        return map;
    }
}
